package com.loveprogrammer.redismq.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName MqUtil
 * @Description 队列服务
 * @Author admin
 * @Date 2023/10/16 11:52
 * @Version 1.0
 */
@Configuration
public class MqUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final String SUFFIX = "_temp";

    public void sendMessage(String topic, String tag, String key, String value, Long timeout, TimeUnit timeUnit){
        String realKey = "mq"+":"+topic +":"+ tag + ":"+  key;
        String realKey2 = "mq"+":"+topic +":"+ tag + ":"+  key + SUFFIX;
        redisTemplate.opsForValue().set(realKey,value,timeout,timeUnit);
        redisTemplate.opsForValue().set(realKey2,value);
    }

    public void sendMessage(String messageKey,String value, Long timeout, TimeUnit timeUnit){
        String realKey = messageKey;
        String realKey2 = messageKey + SUFFIX;
        redisTemplate.opsForValue().set(realKey,value,timeout,timeUnit);
        redisTemplate.opsForValue().set(realKey2,value);
    }

    public String getMessage(String messageKey){
        String realKey = messageKey + SUFFIX;
        return redisTemplate.opsForValue().get(realKey);
    }

    public void releaseMessage(String messageKey){
        redisTemplate.delete(messageKey);
        String realKey = messageKey + SUFFIX;
        redisTemplate.delete(realKey);
    }
}
